<template>
	<view class="page">
		<!-- 地址 -->
		<view class="address-data" v-if="buy_shop_switch==2">
			<view class="address-list" @tap='onAddress'>
				<view  v-if="addressInfo">
					<view class="list">
						<text>{{addressInfo.province}}{{addressInfo.city}}{{addressInfo.area}}</text>
					</view>
					<view class="list">
						<text class="address">{{addressInfo.address}}</text>
					</view>
					<view class="list">
						<text>{{addressInfo.name}}</text>
						<text>{{addressInfo.phone}}</text>
					</view>
					<view class="list">
						<text class="tips">(如果快递不方便接收，您可以选择暂时寄存服务)</text>
					</view>
				</view>
				<view class="flex flex-align flex-bt" v-else style="padding-bottom: 30rpx;">
					<view class="left" >设置收货地址</view>
					<view class="rigth">
						<tui-icon name="arrowright" :size="24" color="#999"></tui-icon>
					</view>
				</view>
			</view>
			<view class="bar">

			</view>
		</view>
		<view class="ziti" v-else>
			<view class="title">
				<text>社区自提</text>
			</view>
			<view class="zitiList">
				<view class="list flex flex-align">
					<view class="left">提货人</view>
					<view class="inpt"><input type="text" placeholder="请输入提货人" v-model="name" /></view>
				</view>
				<view class="list flex flex-align">
					<view class="left">手机号码</view>
					<view class="inpt"><input type="number" placeholder="请输入手机号码" v-model="phone" /></view>
				</view>
				<view class="lists">
					团长信息：{{dataInfos.name}}
				</view>
				<view class="lists">
					提货地点：{{dataInfos.address}}
				</view>
			</view>
		</view>
		<!-- 商品 -->
		<view class="goods-data">
			<view class="goods-title">
				<text>商品信息</text>
			</view>
			<view class="goods-list">
				<view class="list" v-for="(item,index) in order" :key="index">
					<view class="thumb">
						<image :src="item.goods_cover" mode=""></image>
					</view>
					<view class="item">
						<view class="title">
							<text class="name one-omit">{{item.goods_name}}</text>
							<text class="attr">{{item.goods_spec}}</text>
						</view>
						<view class="price-number">
							<!-- <view class="price">
								<text class="min">￥</text>
								<text class="max">{{item.price_selling}}</text>
							</view> -->
							<view class="price" v-if="dataInfo.goods_type!=3">
								<text class="min">￥</text>
								<text class="max">{{item.price_selling}}</text>
							</view>
							<view class="price" v-if="dataInfo.goods_type==3">
								<text class="max">{{Math.trunc(item.price_selling)}}</text><text style="color: #f20;margin-left: 10rpx;font-weight: 700;">礼品卡</text>
							</view>
							<view class="number">
								<text>x {{item.stock_sales}}</text>
							</view>
						</view>
						<!-- <view class="tag">
							<text>支持七天无理由退货</text>
						</view> -->
					</view>
				</view>
			</view>
			<view class="delivery">
				<div class="list">
					<view class="title">配送</view>
					<view class="content">
						<text>{{buy_shop_switch==2?'快递运输':'自提'}}</text>
						<text class="iconfont icon-more"></text>
					</view>
				</div>
				<div class="list">
					<view class="title">留言</view>
					<view class="content">
						<input type="text" placeholder="选填,建议先和商家沟通确认">
					</view>
				</div>
			</view>
		</view>
		<!-- 优惠 -->
		
		<!-- 订单金额 -->
		<view class="order-price">
			<view class="price-list">
				<view class="list">
					<view class="title">
						<text>商品金额</text>
					</view>
					<view class="price" v-if="dataInfo.goods_type!=3">
						<text class="min">￥</text>
						<text class="max">{{dataInfo.amount_total}}</text>
					</view>
					<view class="price" v-if="dataInfo.goods_type==3">
						<text class="max">{{Math.trunc(dataInfo.amount_total)}}</text><text style="margin-left: 10rpx;font-weight: 700;font-size: 24rpx;">礼品卡</text>
					</view>
				</view>
				<view class="list" v-if="dataInfo.coupon_price!=0">
				<!-- <view class="list" @click="openQuan" v-if="dataInfo.coupon_price==0"> -->
					<view class="title">
						<text>优惠券</text>
					</view>
					<view class="price">
						<text style="color: #f20;">- ￥{{dataInfo.coupon_price}}</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部合计提交 -->
		<view class="footer-submit">
			<view class="price" v-if="dataInfo.goods_type!=3">
				<text class="min">￥</text>
				<text class="max">{{dataInfo.amount_real}}</text>
			</view>
			<view class="price" v-if="dataInfo.goods_type==3">
				<text class="max">{{Math.trunc(dataInfo.amount_real)}}</text><text style="color: #f20;margin-left: 10rpx;font-weight: 700;">礼品卡</text>
			</view>
			<view class="submit" @click="onSubmit">
				<text>提交订单</text>
			</view>
		</view>
		<!-- 发票 -->
		<!-- 优惠券 -->
		
		<use-coupon ref="UseCoupon" :data="quanInfo" @finish="getyhq"></use-coupon>
	</view>
</template>

<script>
	import UseCoupon from '../../components/UseCoupon/UseCoupon.vue'
	export default {
		components: {
			// 优惠券
			UseCoupon,
		},
		data() {
			return {
				scrollTop: 0,
				addressInfo:'',
				code:'',
				dataInfo:'',
				order:[],
				quanInfo:[],
				yhqContent:'',
				yhqInfo:'',
				count:0,
				buy_shop_switch:'',
				dataInfos:'',
				name:'',
				phone:'',
				type:''
			};
		},
		onPageScroll(e){
			this.scrollTop = e.scrollTop;
			this.type=e.type
		},
		onLoad(e) {
			this.code =e.order_no
			
		},
		onShow() {
			this.get_data()
			this.getData()
		},
		methods:{
			/**
			 * 提交订单
			 */
			
			getData(){
				let that = this
				that.tui.request("api.auth.center/get", "POST", {}, false, false, true).then((res) => {
					if (res.code == 1) {
						that.dataInfos = res.data.community_pid
						that.buy_shop_switch = res.data.buy_shop_switch
						if(that.buy_shop_switch==2){
							that.getAddress()
						}
					}
				}).catch((res) => {
				
				})
			},
			onSubmit(){
				let that = this
				uni.showLoading()
				that.tui.request("api.auth.order/perfect", "post", {order_no:that.code,code:that.addressInfo.code,coupon_id:that.yhqInfo.id,address_name:that.name,address_phone:that.phone}, false,false, true).then((res) => {
					console.log(res)
					that.tui.request("api.auth.order/payment", "post", {order_no:res.data.order_no,payment_code:that.dataInfo.goods_type==3?'M8052888745606631252':"M8091754246722494828"}, false,false, true).then((ddd) => {
						console.log(ddd)
						// that.tui.toast('支付成功')
						uni.hideLoading()
						if(ddd.code==1){
							if(that.dataInfo.goods_type==3){
								that.tui.toast('支付成功')
								setTimeout(function(){
									uni.redirectTo({
										url:'/pages/PayResult/PayResult?price='+that.count+"&zhifu=礼品卡&goods_type=3",
									})
								},1000)
							}else{
								if(ddd.data.type==2){
									that.tui.toast('支付成功')
									setTimeout(function(){
										uni.redirectTo({
											url:'/pages/PayResult/PayResult?price='+that.count+"&zhifu=微信支付&goods_type=1",
										})
										if(that.type==1){
											that.tui.TongjiLive(1, uni.getStorageSync('room_id'))
										}
									},1000)
								}else{
									uni.requestPayment({
										timeStamp: ddd.data.param.timeStamp,
										nonceStr: ddd.data.param.nonceStr,
										package: ddd.data.param.package,
										signType: ddd.data.param.signType,
										paySign: ddd.data.param.paySign,
									    success: function (res) {
									        console.log('success:' + JSON.stringify(res));
											setTimeout(function(){
												uni.redirectTo({
													url:'/pages/PayResult/PayResult?price='+that.count+"&zhifu=微信支付&goods_type=1",
												})
												if(that.type==1){
													that.tui.TongjiLive(1, uni.getStorageSync('room_id'))
												}
											},1000)
									    },
									    fail: function (err) {
									        console.log('fail:' + JSON.stringify(err));
											if(that.type==1){
												that.tui.TongjiLive(1, uni.getStorageSync('room_id'))
											}
									    }
									});
								}
								
							}
							
							// setTimeout(function(){
								// uni.redirectTo({
								// 	url: '/pages/PayResult/PayResult?price='+that.price+"&zhifu="+that.PayList[that.PayWay].name,
								// })
								// uni.redirectTo({
								// 	url:'/pages/shop/order_list?type=2'
								// })
							// },1000)
						}else{
							setTimeout(function(){
								uni.redirectTo({
									url:'/pages/shop/order_list?type=1'
								})
							},1000)
						}
					}).catch((res) => {})
					
				}).catch((res) => {})
				
			},
			getyhq(e){
				console.log(e)
				if(e){
					this.yhqContent = '满'+e.full_price+'元减'+e.price+'元'
					this.yhqInfo = e
					this.count = (this.dataInfo.amount_real-e.price).toFixed(2)
				}else{
					this.yhqContent =''
					this.count = this.dataInfo.amount_real
				}
				
			},
			openQuan(){
				if(this.quanInfo.length==0){
					return
				}
				this.$refs['UseCoupon'].show()
			},
			getQuan(){
				let that = this
				that.tui.request("api.auth.order/couponOrderList", "GET", {price:that.dataInfo.amount_real}, false,false, true).then((res) => {
					that.quanInfo = res.data.list
				}).catch((res) => {})
			},
			getAddress(){
				let that = this
				that.tui.request("api.auth.address/get", "GET", {page:1}, false,false, true).then((res) => {
					that.addressInfo = res.data.list[0]
				}).catch((res) => {})
			},
			get_data(){
				let that = this
				that.tui.request("api.auth.order/orderDetail", "post", {order_no:that.code}, false, true, false).then((res) => {
					console.log(res)
					that.dataInfo = res.data.info
					that.order = res.data.info.order
					that.count = res.data.info.amount_real
					that.getQuan()
				}).catch((res) => {})
			},
			onAddress() {
				if(this.addressInfo){
					uni.navigateTo({
						url: '/pages/AddressList/AddressList'
					})
				}else{
					uni.navigateTo({
						url: '/pages/AddressEdit/AddressEdit'
					})
				}
			},
      /**
       * 跳转点击
       * @param {String} type 跳转类型
       */
      onSkip(type){
        switch (type){
          case 'address':
            uni.navigateTo({
              url: '/pages/AddressList/AddressList',
            })
            break;
        }
      }
		}
	}
</script>

<style scoped lang="scss">
	@import 'ConfirmOrder.scss';
	
</style>
